
//////////////////////////////////////////////////////////iOS4 HTML5/////////////////////////////////////////////////////////////
function Player(aElement) 
{

    var player1;
    var player2;
    
    try
    {
        
        
        if(aElement)
        {
            player1 = aElement.createElement('audio');
            player2 = aElement.createElement('audio');

            
            this.player = player1;
            this.player1 = player1;
            this.player2 = player2;
            
            //player.setAttribute('autoplay', true);
            this.player.addEventListener('playing', eventplay, false);
            this.player.addEventListener('loadeddata', eventloadeddata, false);
            this.player.addEventListener('durationchange', eventdurationchange, false);
            this.player.addEventListener('loadstart', eventloadstart, false);
            this.player.addEventListener('pause', eventpaused, false);
            this.player.addEventListener('ended', eventended, false);
            
            
            this.startTime = 0;
            this.stopTime=0;
            this.BeginPolling=0;
            this.playerNr = 1;
            
            this.CheckEndTime = function ()
            {
        
                if (this.player != undefined && this.player.currentTime != undefined) 
                {
                    
                    if (this.player.currentTime >= this.stopTime) 
                    {
       
                        try
                        {
                            
                            this.player.pause();
                            clearInterval(this.BeginPolling);
                            fireEvent(aElement,'systemPause');
                        }
                        catch(err)
                        {
                            alert(err);
                        }
                    }


                }
                else 
                {
        
                    clearInterval(BeginPolling);

                }



            }
            
            this.CacheNextSoundFile = function (url)
            {
		/* **************************************iOS4 kan ikke lave to flere objekter af audio objektet***********(endnu)
                if(this.playerNr == 1)
                {
                    try
                    {
                        this.player2.src = url;
		
                        this.player2.load();
			
                        
                    }
                    catch(e)
                    {
                        alert(e);
                    }
                
                }
                else if(this.playerNr == 2)
                {
                    this.player1.src = url;

                    this.player1.load();
                
                }
		*/

            }
            
            this.isCached = function(url)
            {
                var returnV = false;
		
		/*
                if(this.playerNr == 1)
                {

                    if(this.player2.src.indexOf(url) != -1)
                    {
                    
                      returnV = true;
                    }
  
                }
                else if(this.playerNr == 2)
                {
                    if(this.player1.src.indexOf(url) != -1)
                    {
                      returnV = true;
                    }
                
                }
            
            	*/
                return returnV;
            
            
            }
            this.SwitchPlayer = function()
            {
            
                try
                {
                    if(this.playerNr == 1)
                    {
                        
                       
                        this.player.pause();
                        this.player = this.player2;
			
                        this.playerNr = 2;

                    }
                    else if (this.playerNr == 2)
                    {
                        this.player.pause();
                        this.player = this.player1;
                        this.playerNr = 1;
                    }
                }
                catch(e)
                {
                    alert(e.message);
                }
                
                
            
            }
            

            
            
            
            
            
        }
    }
    catch(e)
    {
        alert(e.message);
    
    }
    
}


function eventplay()
{

    
}

function eventloadeddata()
{
   
    Player.player.play();
}

function eventdurationchange() 
{

    

}

function eventpaused() 
{
    
    
    
}

function eventloadstart() 
{
    
  
   
}

function eventended()
{
    
}

function fireEvent(element,event)
{
    
    if (document.createEventObject)
    {
    // dispatch for IE
        var evt = document.createEventObject();
        return element.fireEvent('on'+event,evt)
    }
    else
    {
    // dispatch for firefox + others
        var evt = document.createEvent("HTMLEvents");
        evt.initEvent(event, false, true ); // event type,bubbling,cancelable
        return !element.dispatchEvent(evt);
    }
}
